<?php


namespace app\admin\controller;
use app\admin\common\controller\Base;
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;
use think\Db;
use think\facade\Request;
use app\admin\common\model\User as UserModel;
use think\facade\Session;
use think\Model;

class User extends Base
{
    //登录
    public function login()
    {
        $this->assign('title','管理员登录');
        return $this->fetch();
    }

    public function checkLogin()
    {
        $data=Request::param();
        $map[]=['name','=',$data['name']];
        $map[]=['password','=',sha1($data['password'])];
        $admin=UserModel::where($map)->find();
        if(!is_null($admin)){
            Session::set('admin_id',$admin['id']);
            Session::set('admin_name',$admin['name']);
            Session::set('admin_level',$admin['is_admin']);
            $this->success('登录成功','admin/index/index');
        }
        $this->error('登录失败');
    }


    //用户列表
    public function userList()
    {
        $admin_id=Session::get('admin_id');
        $admin_level=Session::get('admin_level');
        $userList=UserModel::where(['id'=>$admin_id,'is_admin'=>$admin_level])->select();
        if($admin_level==1){
            $userList=UserModel::all();
        }
        $this->assign('userList',$userList);
        $this->assign('title','用户列表');
        return $this->fetch();
    }


    //用户修改
    public function userEdit()
    {
        $this->isLogin();
        $id=Session::get('admin_id');
        $user=UserModel::get($id);
        $this->assign('user',$user);
        if (Request::isPost()){
            $data=Request::post();
            $data['password']=sha1($data['password']);
            UserModel::where('id',$id)->update($data);
            $this->success('修改成功','user/userList');
        }
        $this->assign('title','用户修改');
        return $this->fetch();
    }

    //绑定邮箱
    public function bindEmail()
    {
        $id=Session::get('admin_id');
        $user=UserModel::get($id);
        $this->assign('email',$user['email']);
        $this->assign('title','绑定邮箱');
        return $this->fetch();
    }

    //发送验证码
    public function sendEmail(){
        $Code='';
        $arr=[3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','j','k','o','p','q','w','e','A','B','C','D','E','F','G','H','M','M'];
        for ($i=0;$i<5;$i++){
            $Code.=$arr[rand(0,count($arr)-1)];
        }
        Session::set('code',$Code);
        $toemail=Request::param('toemail');
        $mail=new PHPMailer();
        try {
            $mail->CharSet ="UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = 'smtp.qq.com';                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = '951972210@qq.com';                // SMTP 用户名  即邮箱的用户名
            $mail->Password = 'icyeqwpslrcmbbdi';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
            $mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持
            $mail->setFrom('951972210@qq.com', 'Mailer');  //发件人
            $mail->addAddress($toemail);  // 可添加多个收件人
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = '请保留好您的验证码';
            $mail->Body    = $Code;
            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';

            $mail->send();
            echo "发送成功";
        } catch (Exception $e) {
            echo '邮件发送失败: ', $mail->ErrorInfo;
        }
    }



    //修改邮箱
    public function changeEmail(){
        if(Request::isPost()){
            $checkCode=Request::param('code');
            $code=Session::get('code');
            if($checkCode!=$code){
                $this->error('验证码错误');
            }else{
                $id=Session::get('admin_id');
                $email=Request::param('email');
                UserModel::where('id',$id)->update(['email'=>$email]);
                 $this->redirect('user/bindemail');
            }
        }
    }


    //检查验证码
    public function checkCode(){
        if (Request::isAjax()){
            $checkcode=Request::param('code');
            $code=Session::get('code');
            if($checkcode==$code){
                return ['status'=>1,'验证成功'];
            }else{
                return ['status'=>0,'验证码错误'];
            }
        }
    }





    //退出登录
    public function logout(){
        Session::delete('admin_id');
        Session::delete('admin_name');
        Session::delete('admin_level');
        $this->success('注销成功','admin/user/login');
    }

}